package org.example.kaobei.deep;

/**
 * 深拷贝示例
 */
public class DeepCopy {

    public static void main(String[] args) throws CloneNotSupportedException {
        TeacherDeep teacher = new TeacherDeep();
        teacher.setName("riemann");
        teacher.setAge(28);

        StudentDeep student1 = new StudentDeep();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        StudentDeep student2 = (StudentDeep) student1.clone();
        System.out.println("-------------拷贝后-------------");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("-------------修改老师的信息后-------------");
        // 修改老师的信息
        teacher.setName("jack");

        // student1的teacher为： jack
        //student2的teacher为： riemann
        // 结果分析：
        //两个引用student1和student2指向不同的两个对象，
        // 两个引用student1和student2中的两个teacher引用指向的是两个对象，但对teacher对象的修改只能影响student1对象,所以说是深拷贝。
        System.out.println("student1的teacher为： " + student1.getTeacher().getName());
        System.out.println("student2的teacher为： " + student2.getTeacher().getName());
    }
}